/* eslint-disable */
import legacyPlugin from '@vitejs/plugin-legacy';
import {
  viteMockServe
} from 'vite-plugin-mock';
import {
  viteExternalsPlugin
} from 'vite-plugin-externals';
import * as path from 'path';
import vuePlugin from '@vitejs/plugin-vue';
import { createHtmlPlugin } from "vite-plugin-html";
// @see https://cn.vitejs.dev/config/
export default ({
  command,
  mode
}) => {
  let rollupOptions = {};


  let optimizeDeps = {};


  let alias = {
    '.git': path.resolve(__dirname, './.git'),
    '.husky': path.resolve(__dirname, './.husky'),
    '.vscode': path.resolve(__dirname, './.vscode'),
    'dist': path.resolve(__dirname, './dist'),
    'koa': path.resolve(__dirname, './koa'),
    'node_modules': path.resolve(__dirname, './node_modules'),
    'nuxt-demo': path.resolve(__dirname, './nuxt-demo'),
    'public': path.resolve(__dirname, './public'),
    'src': path.resolve(__dirname, './src'),
    '@': path.resolve(__dirname, './src'),
    'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
    '_c': path.resolve(__dirname, './src/components'),
  }

  let proxy = {
    '/news': {
      "target": "https://api.jisuapi.com/news/get",
      "changeOrigin": true,
      "pathRewrite": {
        "^/news": ""
      }
    },
  }

  // todo 替换为原有变量
  let define = {}

  let esbuild = {}

  return {
    base: './', // index.html文件所在位置
    root: './', // js导入的资源路径，src
    resolve: {
      alias,
    },
    define: define,
    server: {
      // 代理
      proxy,
      port: 8081
    },
    build: {
      target: 'es2015',
      minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
      manifest: false, // 是否产出maifest.json
      sourcemap: false, // 是否产出soucemap.json
      outDir: 'build', // 产出目录
      rollupOptions,
    },
    esbuild,
    optimizeDeps,
    plugins: [
      legacyPlugin({
        targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
      }), viteMockServe({
        mockPath: 'mock',
        localEnabled: command === 'serve',
      }), viteExternalsPlugin({
        "markdown-pdf": "require(\"markdown-pdf\")"
      }), vuePlugin(),
      createHtmlPlugin({
        minify: true,
        template: '/public/index.html',
        entry: 'src/main.ts',
        inject: {
          data: {
            htmlWebpackPlugin: {
              options: {
                title: '练手例子',
              }
            }
          }
        }
      })
    ],
    css: {
      preprocessorOptions: {
        less: {
          // 支持内联 JavaScript
          javascriptEnabled: true,
        },
      },
    },
  };
};